/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.homesoft.catalogman.gui;

import com.homesoft.catalogman.entity.HmObjectType;
import com.homesoft.catalogman.entity.HmTaxonomy;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JPanel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreePath;

/**
 *
 * @author Bao Nguyen
 */
public class Main_01_2 extends JPanel {

    Object m_htSelectedItem;
    Map<Integer, DefaultMutableTreeNode> m_dicItem;

    /**
     * Creates new form Main_01_2
     */
    public Main_01_2() {
        initComponents();
        m_dicItem = new HashMap<>();
    }

    /**
     * Nạp danh sách catalog
     *
     * @param lstType
     */
    public void bindCatalog(List<HmObjectType> lstType) {
        DefaultTreeModel mDefault = (DefaultTreeModel) treeCategory.getModel();
        DefaultMutableTreeNode root = (DefaultMutableTreeNode) mDefault.getRoot();
        root.removeAllChildren();
        for (HmObjectType htItem : lstType) {
            DefaultMutableTreeNode node = new DefaultMutableTreeNode(htItem);
            root.add(node);
            m_dicItem.put(-htItem.getId(), node);

        }
        treeCategory.setModel(mDefault);
        treeCategory.setRootVisible(false);
        treeCategory.updateUI();
    }

    /**
     * Nạp dữ liệu phân loại
     *
     * @param lstData Danh sách taxonomy
     */
    public void bindTaxonomy(List<HmTaxonomy> lstData) {
        //TODO: sort list here
        DefaultTreeCellRenderer cRender = new DefaultTreeCellRenderer();
        cRender.setOpenIcon(null);
        cRender.setClosedIcon(null);
        cRender.setLeafIcon(null);
        //Render.setIcon(null);
        treeCategory.setCellRenderer(cRender);
        DefaultTreeModel mDefault = (DefaultTreeModel) treeCategory.getModel();
        DefaultMutableTreeNode root = (DefaultMutableTreeNode) mDefault.getRoot();
        //root.removeAllChildren();
        for (HmTaxonomy htItem : lstData) {
            DefaultMutableTreeNode node = new DefaultMutableTreeNode(htItem);

            //level 1 taxonomy
            if (m_dicItem.containsKey(-htItem.getOptionNo())) {
                m_dicItem.get(-htItem.getOptionNo()).add(node);
                m_dicItem.put(htItem.getId(), node);
                continue;
            }

            //taxonomy level >1
            if (m_dicItem.containsKey(htItem.getParent())) {
                //if there is no parent
                //add to parent node
                m_dicItem.get(htItem.getParent()).add(node);
            } else {
                root.add(node);
                m_dicItem.put(htItem.getId(), node);
            }
        }
        treeCategory.setModel(mDefault);
        treeCategory.setRootVisible(false);
        treeCategory.updateUI();
    }

    /**
     * Lấy về phần tử được chọn
     *
     * @return selected object
     */
    public Object getSelectedItem() {
        return m_htSelectedItem;
    }

    /**
     * Set selected item by ID
     *
     * @param iCateId category ID. If iCateId less than 0, it mean that
     * selection would be HmObjectType, otherwise selection would be HmTaxonomy
     */
    public void setSelectedItem(int iCateId) {
        DefaultMutableTreeNode root = (DefaultMutableTreeNode) treeCategory.getModel().getRoot();
        if (!root.children().hasMoreElements()) {
            return;
        }
        Object objChild = null;
        while ((objChild = root.children().nextElement()) != null) {
            HmObjectType hmType = (HmObjectType) ((DefaultMutableTreeNode) objChild).getUserObject();
            if (hmType.getId() == iCateId) {
                treeCategory.setSelectionPath(
                        new TreePath(((DefaultMutableTreeNode) objChild).getPath())
                );
            }
        }
    }

    /**
     * Đăng ký sự kiện cho nút
     *
     * @param evt
     */
    public void addButtonListener(ActionListener evt) {
        btnOK.addActionListener(evt);
    }

    /**
     * Xóa sự kiện của nút
     *
     * @param evt
     */
    public void removeButtonLister(ActionListener evt) {
        btnOK.removeActionListener(evt);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        pnlHeader = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        pnlTree = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        treeCategory = new javax.swing.JTree();
        btnOK = new org.jdesktop.swingx.JXButton();

        setName("pnlSidebarContent"); // NOI18N
        setOpaque(false);
        setLayout(new java.awt.GridBagLayout());

        pnlHeader.setBorder(null);
        pnlHeader.setOpaque(false);

        jLabel1.setFont(jLabel1.getFont());
        jLabel1.setForeground(new java.awt.Color(245, 245, 245));
        jLabel1.setText("Catalog: ");

        javax.swing.GroupLayout pnlHeaderLayout = new javax.swing.GroupLayout(pnlHeader);
        pnlHeader.setLayout(pnlHeaderLayout);
        pnlHeaderLayout.setHorizontalGroup(
            pnlHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlHeaderLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(127, Short.MAX_VALUE))
        );
        pnlHeaderLayout.setVerticalGroup(
            pnlHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlHeaderLayout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.ipadx = 115;
        gridBagConstraints.ipady = -8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        add(pnlHeader, gridBagConstraints);

        pnlTree.setOpaque(false);

        treeCategory.setBackground(new java.awt.Color(225, 225, 225));
        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("root");
        treeCategory.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        treeCategory.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                treeCategoryValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(treeCategory);

        btnOK.setText("Apply");

        javax.swing.GroupLayout pnlTreeLayout = new javax.swing.GroupLayout(pnlTree);
        pnlTree.setLayout(pnlTreeLayout);
        pnlTreeLayout.setHorizontalGroup(
            pnlTreeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE)
            .addGroup(pnlTreeLayout.createSequentialGroup()
                .addGap(61, 61, 61)
                .addComponent(btnOK, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnlTreeLayout.setVerticalGroup(
            pnlTreeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlTreeLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 363, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnOK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 12, Short.MAX_VALUE))
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.ipadx = 51;
        gridBagConstraints.ipady = 12;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 0);
        add(pnlTree, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void treeCategoryValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_treeCategoryValueChanged
        m_htSelectedItem = ((DefaultMutableTreeNode) evt.getPath()
                .getLastPathComponent()).getUserObject();
    }//GEN-LAST:event_treeCategoryValueChanged


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.jdesktop.swingx.JXButton btnOK;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel pnlHeader;
    private javax.swing.JPanel pnlTree;
    private javax.swing.JTree treeCategory;
    // End of variables declaration//GEN-END:variables

}
